/*
 * @Author: 260156 260156@gree.com.cn
 * @Date: 2022-08-17 09:37:06
 * @LastEditors: 260156 260156@gree.com.cn
 * @LastEditTime: 2022-10-24 15:29:44
 * @FilePath: \gree-zst-web\src\utils\request\RequestMehods.ts
 * @Description:
 *
 * Copyright (c) 2022 by 260156 260156@gree.com.cn, All Rights Reserved.
 */
import type { AxiosRequestConfig } from "axios";
import type { DownloadRequestResult, DownloadRequestOptions } from "#/request";

export class RequestMethods {
  request: <T = any>(config: AxiosRequestConfig) => Promise<T>;

  constructor(request: <T = any>(config: AxiosRequestConfig) => Promise<T>) {
    this.request = request;
  }

  get<T = any>(config: AxiosRequestConfig): Promise<T> {
    return this.request({ ...config, method: "GET" });
  }

  post<T = any>(config: AxiosRequestConfig): Promise<T> {
    return this.request({ ...config, method: "POST" });
  }

  put<T = any>(config: AxiosRequestConfig): Promise<T> {
    return this.request({ ...config, method: "PUT" });
  }

  delete<T = any>(config: AxiosRequestConfig): Promise<T> {
    return this.request({ ...config, method: "DELETE" });
  }
}

type DownloadRequestParams = (
  config: AxiosRequestConfig,
  options?: DownloadRequestOptions
) => Promise<DownloadRequestResult>;

export class DownloadRequestMethods {
  request: DownloadRequestParams;

  constructor(request: DownloadRequestParams) {
    this.request = request;
  }

  get(
    config: AxiosRequestConfig,
    options?: DownloadRequestOptions
  ): Promise<DownloadRequestResult> {
    return this.request({ ...config, method: "GET" }, options);
  }

  post(
    config: AxiosRequestConfig,
    options?: DownloadRequestOptions
  ): Promise<DownloadRequestResult> {
    return this.request({ ...config, method: "POST" }, options);
  }

  put(
    config: AxiosRequestConfig,
    options?: DownloadRequestOptions
  ): Promise<DownloadRequestResult> {
    return this.request({ ...config, method: "PUT" }, options);
  }

  delete(
    config: AxiosRequestConfig,
    options?: DownloadRequestOptions
  ): Promise<DownloadRequestResult> {
    return this.request({ ...config, method: "DELETE" }, options);
  }
}
